package net.esj.basic.template;

import java.io.StringWriter;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public abstract class AbstractTemplateEngine implements TemplateEngine {

	public abstract String getTemplatePath();
	
	public abstract String getEngineType();
	
	public String run(Map context ,String template)throws Exception{
		return executeVelocity(context,template);
	}
	
	private String executeVelocity(Map root ,String tempfile)throws Exception{
		
//		Properties p = new Properties();
//		p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,getClass().getResource("/").getPath());
		Velocity.init(getClass().getResource("/velocity.properties").getPath());
		
		
		VelocityContext context = new VelocityContext(root);
		org.apache.velocity.Template template = null;
		
//		File f = new File(getTemplatePath()+tempfile);
//		if(f.exists()){
			template = Velocity.getTemplate(tempfile);
//		}
	    
		StringWriter sw = new StringWriter();
		template.merge( context, sw );
		System.out.print(sw.toString());
		return sw.toString();
	}
}
